package com.systemic.service.impl;

import com.systemic.mapper.FriendMapper;
import com.systemic.mapper.LoveBankCountMapper;
import com.systemic.mapper.LoveBankMapper;
import com.systemic.mapper.UserInfoMapper;
import com.systemic.pojo.Friend;
import com.systemic.pojo.LoveBank;
import com.systemic.pojo.LoveBankCount;
import com.systemic.pojo.UserInfo;
import com.systemic.service.LoveBankService;

import com.systemic.utils.FileUtils;
import com.systemic.vo.LoveBankInfo;
import com.systemic.vo.LoveBankLog;
import com.systemic.vo.PageVo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: LoveBankServiceImpl
 * @Description:
 * @Author:
 * @CreateTime: 2024-02-03 20:20
 * @Version:
 **/
@Service
@PropertySource("classpath:/properties/image.properties")
public class LoveBankServiceImpl implements LoveBankService {

    @Resource
    private FriendMapper friendMapper;

    @Resource
    private LoveBankMapper loveBankMapper;

    @Resource
    private LoveBankCountMapper loveBankCountMapper;

    @Resource
    private UserInfoMapper userInfoMapper;

    @Value("${image.depositImg}")
    private String depositImg;
    @Value("${image.depositURL}")
    private String depositURL;

    @Value("${image.withdrawImg}")
    private String withdrawImg;
    @Value("${image.withdrawURL}")
    private String withdrawURL;



    /**
     * 存款
     * @param img
     * @param loveBank
     * @return
     */
    @Override
    @Transactional
    public int deposit(MultipartFile img, LoveBank loveBank) {
        int row = 0;
        if (!img.isEmpty()){
            // 执行上传文件
            String url = FileUtils.uploadImage(img, depositImg, depositURL);
            loveBank.setImg(url);
        }
        Date date = new Date();
        loveBank.setGmtCreate(date);
        row = loveBankMapper.insert(loveBank)+row;
        LoveBankCount loveBankCount = loveBankCountMapper.selectByUid(loveBank.getUid());
        if (loveBankCount!=null){
            if (loveBankCount.getDeposit()==null){
                loveBankCount.setDeposit(1);
            } else if(loveBankCount.getDeposit()==0) {
                loveBankCount.setDeposit(1);
            } else {
                loveBankCount.setDeposit(loveBankCount.getDeposit()+1);
            }
            loveBankCount.setCountTime(date);
            row = loveBankCountMapper.update(loveBankCount)+row;
        } else {
            // 没有那就新建一个
            LoveBankCount count = new LoveBankCount();
            count.setUid(loveBank.getUid());
            count.setDeposit(1);
            count.setCountTime(date);
            row = loveBankCountMapper.insert(count)+row;
        }
        return row;
    }

    /**
     * 取款
     * @param img
     * @param loveBank
     * @return
     */
    @Override
    @Transactional
    public int withdraw(MultipartFile img, LoveBank loveBank) {
        int row = 0;
        if (!img.isEmpty()){
            // 执行上传文件
            String url = FileUtils.uploadImage(img, withdrawImg, withdrawURL);
            loveBank.setImg(url);
        }
        Date date = new Date();
        loveBank.setGmtCreate(date);
        row = loveBankMapper.insert(loveBank)+row;
        LoveBankCount loveBankCount = loveBankCountMapper.selectByUid(loveBank.getUid());
        if (loveBankCount!=null){
            if (loveBankCount.getWithdraw()==null){
                loveBankCount.setWithdraw(1);
            } else if(loveBankCount.getWithdraw()==0) {
                loveBankCount.setWithdraw(1);
            } else {
                loveBankCount.setWithdraw(loveBankCount.getWithdraw()+1);
            }
            loveBankCount.setCountTime(date);
            row = loveBankCountMapper.update(loveBankCount)+row;
        } else {
            // 没有那就新建一个
            LoveBankCount count = new LoveBankCount();
            count.setUid(loveBank.getUid());
            count.setWithdraw(1);
            count.setCountTime(date);
            row = loveBankCountMapper.insert(count)+row;
        }
        return row;
    }

    /**
     * 存取记录
     * @param pageVo
     * @return
     */
    @Override
    @Transactional
    public List<LoveBankLog> selectList(PageVo pageVo) {
        // 获取当前页数和每页显示的数据条数
        Integer pageNo = pageVo.getPageNo();
        Integer pageSize = pageVo.getPageSize();
        // 计算OFFSET值
        Integer offset = (pageNo - 1) * pageSize;
        Integer uid = pageVo.getUid();
        List<LoveBank> loveBankList = loveBankMapper.selectListByUid(uid,offset,pageSize);
        List<LoveBankLog> res = new ArrayList<>();

        if (!loveBankList.isEmpty()){
            for (LoveBank loveBank:loveBankList) {
                LoveBankLog log = new LoveBankLog();
                Integer fid = loveBank.getFid();
                Friend friend = friendMapper.selectByUidAndFid(uid,fid);
                UserInfo userInfo = userInfoMapper.selectUserInfoByUid(fid);
                log.setId(loveBank.getId());
                log.setUid(loveBank.getUid());
                log.setFid(fid);
                log.setFriendName(friend.getNickname());
                log.setFriendAvatar(userInfo.getAvatar());
                log.setTypesOf(loveBank.getTypesOf());
                log.setGmtCreate(loveBank.getGmtCreate());
                res.add(log);
            }
        }

        return res;
    }

    /**
     * 存取年统计
     * @param uid
     * @return
     */
    @Override
    public LoveBankCount selectCount(Integer uid) {
        return loveBankCountMapper.selectByUid(uid);
    }

    /**
     * 存款详情
     * @param id
     * @return
     */
    @Override
    public LoveBankInfo loveBankInfo(Integer id) {

        LoveBank loveBank = loveBankMapper.selectById(id);
        LoveBankInfo res = new LoveBankInfo();

        Integer uid = loveBank.getUid();
        Integer fid = loveBank.getFid();

        if (loveBank!=null){

            Friend friend = friendMapper.selectByUidAndFid(uid,fid);
            UserInfo userInfo = userInfoMapper.selectUserInfoByUid(uid);
            UserInfo friendInfo = userInfoMapper.selectUserInfoByUid(fid);

            res.setId(loveBank.getId());
            res.setUid(uid);
            res.setUserName(userInfo.getUname());
            res.setUserAvatar(userInfo.getAvatar());

            res.setFid(fid);
            res.setFriendName(friend.getNickname());
            res.setFriendAvatar(friendInfo.getAvatar());

            if (loveBank.getImg()!=null){
                res.setImg(loveBank.getImg());
            }
            if (loveBank.getMsg()!=null){
                res.setMsg(loveBank.getMsg());
            }
            res.setGmtCreate(loveBank.getGmtCreate());
        }
        return res;
    }

}
